* This Stata-Do-File replicates the results from the article
* Don’t Hate the Mayor, Hate the Game – an Analysis of Hate Speech on Social Media Against Mayors in Baden-Wuerttemberg
* Rafael Bauschke & Sebastian Jäckle
* in Policy and Internet 2023

clear all
version 15
set scheme plottigblind

* load packages
ssc install outreg2, replace
ssc install coefplot, replace
ssc install mimrgns, replace
ssc install catplot, replace
ssc install estout, replace
ssc install combomarginsplot, replace
ssc install tab_chi, replace
net install grc1leg, from(http://www.stata.com/users/vwiggins) replace

* set working directory
cd "C:\Users\Sebastian\Dropbox\Bürgermeister_Soziale Medien\Replication"
*cd "YOUR_PATH\Replication"

* Load dataset
use "data\mayors_survey.dta"

**** Constructing the dataset for analysis

* Code reactions to hate speech (based on variable "Anfeindungen_Reaktion"
gen reaction_hate = .

 replace reaction_hate = 1 in 6
 replace reaction_hate = 2 in 7
 replace reaction_hate = 2 in 10
 replace reaction_hate = 1 in 12
 replace reaction_hate = 1 in 13
 replace reaction_hate = 1 in 14
 replace reaction_hate = 3 in 15
 replace reaction_hate = 1 in 16
 replace reaction_hate = 2 in 21
 replace reaction_hate = 2 in 22
 replace reaction_hate = 4 in 15
 replace reaction_hate = 1 in 25
 replace reaction_hate = 2 in 26
 replace reaction_hate = 1 in 29
 replace reaction_hate = 2 in 30
 replace reaction_hate = 1 in 33
 replace reaction_hate = 2 in 35
 replace reaction_hate = 1 in 37
 replace reaction_hate = 2 in 38
 replace reaction_hate = 1 in 40
 replace reaction_hate = 2 in 41
 replace reaction_hate = 2 in 42
 replace reaction_hate = 1 in 44
 replace reaction_hate = 1 in 45
 replace reaction_hate = 2 in 46
 replace reaction_hate = 1 in 47
 replace reaction_hate = 4 in 48
 replace reaction_hate = 2 in 49
 replace reaction_hate = 1 in 50
 replace reaction_hate = 1 in 54
 replace reaction_hate = 2 in 55
 replace reaction_hate = 1 in 56
 replace reaction_hate = 2 in 64
 replace reaction_hate = 1 in 71
 replace reaction_hate = 4 in 72
 replace reaction_hate = 1 in 78
 replace reaction_hate = 2 in 79
 replace reaction_hate = 2 in 80
 replace reaction_hate = 1 in 81
 replace reaction_hate = 1 in 84
 replace reaction_hate = 2 in 85
 replace reaction_hate = 1 in 87
 replace reaction_hate = 1 in 89
 replace reaction_hate = 1 in 90
 replace reaction_hate = 1 in 91
 replace reaction_hate = 1 in 92
 replace reaction_hate = 2 in 94
 replace reaction_hate = 1 in 95
 replace reaction_hate = 1 in 96
 replace reaction_hate = 3 in 100
 replace reaction_hate = 3 in 102
 replace reaction_hate = 4 in 103
 replace reaction_hate = 2 in 104
 replace reaction_hate = 1 in 108
 replace reaction_hate = 1 in 109
 replace reaction_hate = 4 in 110
 replace reaction_hate = 1 in 112
 replace reaction_hate = 1 in 113
 replace reaction_hate = 2 in 115
 replace reaction_hate = 1 in 117
 replace reaction_hate = 1 in 119
 replace reaction_hate = 1 in 121
 replace reaction_hate = 2 in 123
 replace reaction_hate = 1 in 124
 replace reaction_hate = 1 in 128
 replace reaction_hate = 2 in 131
 replace reaction_hate = 2 in 133
 replace reaction_hate = 2 in 135
 replace reaction_hate = 2 in 137
 replace reaction_hate = 4 in 139
 replace reaction_hate = 2 in 141
 replace reaction_hate = 4 in 142
 replace reaction_hate = 4 in 143
 replace reaction_hate = 1 in 144
 replace reaction_hate = 1 in 145
 replace reaction_hate = 1 in 147
 replace reaction_hate = 1 in 149
 replace reaction_hate = 1 in 152
 replace reaction_hate = 1 in 155
 replace reaction_hate = 1 in 159
 replace reaction_hate = 2 in 160
 replace reaction_hate = 2 in 161
 replace reaction_hate = 1 in 162
 replace reaction_hate = 3 in 163
 replace reaction_hate = 1 in 164
 replace reaction_hate = 1 in 165
 replace reaction_hate = 1 in 173
 replace reaction_hate = 2 in 173
 replace reaction_hate = 1 in 175
 replace reaction_hate = 2 in 183
 replace reaction_hate = 1 in 185
 replace reaction_hate = 2 in 186
 replace reaction_hate = 1 in 187
 replace reaction_hate = 2 in 188
 replace reaction_hate = 1 in 189
 replace reaction_hate = 3 in 190
 replace reaction_hate = 1 in 192
 replace reaction_hate = 1 in 200
 replace reaction_hate = 1 in 201
 replace reaction_hate = 2 in 203
 replace reaction_hate = 1 in 205
 replace reaction_hate = 4 in 209
 replace reaction_hate = 1 in 210
 replace reaction_hate = 1 in 211
 replace reaction_hate = 2 in 212
 replace reaction_hate = 1 in 213
 replace reaction_hate = 1 in 214
 replace reaction_hate = 1 in 215
 replace reaction_hate = 2 in 217
 replace reaction_hate = 2 in 220
 replace reaction_hate = 2 in 226
 replace reaction_hate = 1 in 227
 replace reaction_hate = 1 in 229
 replace reaction_hate = 1 in 232
 replace reaction_hate = 1 in 233
 replace reaction_hate = 1 in 234
 replace reaction_hate = 4 in 238
 replace reaction_hate = 1 in 244
 replace reaction_hate = 1 in 247
 replace reaction_hate = 1 in 248
 replace reaction_hate = 1 in 249
 replace reaction_hate = 1 in 251
 replace reaction_hate = 2 in 253
 replace reaction_hate = 1 in 263
 replace reaction_hate = 1 in 264
 replace reaction_hate = 1 in 265
 replace reaction_hate = 2 in 266
 replace reaction_hate = 1 in 267
 replace reaction_hate = 1 in 270
 replace reaction_hate = 1 in 271
 replace reaction_hate = 2 in 272
 replace reaction_hate = 2 in 273
 replace reaction_hate = 1 in 275
 replace reaction_hate = 1 in 280
 replace reaction_hate = 1 in 281
 replace reaction_hate = 2 in 281
 replace reaction_hate = 1 in 284
 replace reaction_hate = 1 in 287
 replace reaction_hate = 4 in 289
 replace reaction_hate = 4 in 294
 replace reaction_hate = 1 in 296
 replace reaction_hate = 1 in 300
 replace reaction_hate = 3 in 302
 replace reaction_hate = 1 in 303
 replace reaction_hate = 1 in 306
 replace reaction_hate = 1 in 307
 replace reaction_hate = 2 in 311
 replace reaction_hate = 3 in 314
 replace reaction_hate = 2 in 316
 replace reaction_hate = 4 in 320
 replace reaction_hate = 1 in 323
 replace reaction_hate = 2 in 327
 replace reaction_hate = 2 in 328
 replace reaction_hate = 1 in 330
 replace reaction_hate = 1 in 331
 replace reaction_hate = 1 in 332
 replace reaction_hate = 2 in 333
 replace reaction_hate = 2 in 334
 replace reaction_hate = 1 in 335
 replace reaction_hate = 1 in 340
 replace reaction_hate = 2 in 343
 replace reaction_hate = 4 in 344
 replace reaction_hate = 1 in 346
 replace reaction_hate = 2 in 350
 replace reaction_hate = 2 in 354
 replace reaction_hate = 3 in 355
 replace reaction_hate = 1 in 356
 replace reaction_hate = 1 in 359
 replace reaction_hate = 3 in 360
 replace reaction_hate = 1 in 365
 replace reaction_hate = 1 in 366
 replace reaction_hate = 1 in 369
 replace reaction_hate = 1 in 371
 replace reaction_hate = 3 in 373
 
 gen risk_shitstorm = 0
 gen risk_fakenews = 0
 gen risk_time = 0
 gen risk_dataprivacy = 0
  
  
replace risk_shitstorm = 1 in 2
replace risk_shitstorm = 1 in 10
replace risk_shitstorm = 1 in 17
replace risk_shitstorm = 1 in 19
replace risk_shitstorm = 1 in 20
replace risk_shitstorm = 1 in 22
replace risk_shitstorm = 1 in 24
replace risk_shitstorm = 1 in 26
replace risk_shitstorm = 1 in 28
replace risk_shitstorm = 1 in 29
replace risk_shitstorm = 1 in 3
replace risk_shitstorm = 1 in 30
replace risk_shitstorm = 1 in 31
replace risk_shitstorm = 1 in 32
replace risk_shitstorm = 1 in 37
replace risk_shitstorm = 1 in 38
replace risk_shitstorm = 1 in 41
replace risk_shitstorm = 1 in 42
replace risk_shitstorm = 1 in 48
replace risk_shitstorm = 1 in 7
replace risk_shitstorm = 1 in 100
replace risk_shitstorm = 1 in 102
replace risk_shitstorm = 1 in 103
replace risk_shitstorm = 1 in 104
replace risk_shitstorm = 1 in 105
replace risk_shitstorm = 1 in 107
replace risk_shitstorm = 1 in 110
replace risk_shitstorm = 1 in 111
replace risk_shitstorm = 1 in 115
replace risk_shitstorm = 1 in 116
replace risk_shitstorm = 1 in 123
replace risk_shitstorm = 1 in 124
replace risk_shitstorm = 1 in 130
replace risk_shitstorm = 1 in 133
replace risk_shitstorm = 1 in 49
replace risk_shitstorm = 1 in 53
replace risk_shitstorm = 1 in 55
replace risk_shitstorm = 1 in 58
replace risk_shitstorm = 1 in 60
replace risk_shitstorm = 1 in 79
replace risk_shitstorm = 1 in 80
replace risk_shitstorm = 1 in 82
replace risk_shitstorm = 1 in 84
replace risk_shitstorm = 1 in 85
replace risk_shitstorm = 1 in 92
replace risk_shitstorm = 1 in 93
replace risk_shitstorm = 1 in 95
replace risk_shitstorm = 1 in 97
replace risk_shitstorm = 1 in 136
replace risk_shitstorm = 1 in 137
replace risk_shitstorm = 1 in 139
replace risk_shitstorm = 1 in 142
replace risk_shitstorm = 1 in 143
replace risk_shitstorm = 1 in 145
replace risk_shitstorm = 1 in 155
replace risk_shitstorm = 1 in 161
replace risk_shitstorm = 1 in 164
replace risk_shitstorm = 1 in 165
replace risk_shitstorm = 1 in 168
replace risk_shitstorm = 1 in 180
replace risk_shitstorm = 1 in 183
replace risk_shitstorm = 1 in 185
replace risk_shitstorm = 1 in 186
replace risk_shitstorm = 1 in 187
replace risk_shitstorm = 1 in 191
replace risk_shitstorm = 1 in 192
replace risk_shitstorm = 1 in 207
replace risk_shitstorm = 1 in 212
replace risk_shitstorm = 1 in 213
replace risk_shitstorm = 1 in 217
replace risk_shitstorm = 1 in 225
replace risk_shitstorm = 1 in 226
replace risk_shitstorm = 1 in 229
replace risk_shitstorm = 1 in 233
replace risk_shitstorm = 1 in 246
replace risk_shitstorm = 1 in 249
replace risk_shitstorm = 1 in 250
replace risk_shitstorm = 1 in 251
replace risk_shitstorm = 1 in 256
replace risk_shitstorm = 1 in 263
replace risk_shitstorm = 1 in 266
replace risk_shitstorm = 1 in 267
replace risk_shitstorm = 1 in 268
replace risk_shitstorm = 1 in 270
replace risk_shitstorm = 1 in 271
replace risk_shitstorm = 1 in 272
replace risk_shitstorm = 1 in 274
replace risk_shitstorm = 1 in 280
replace risk_shitstorm = 1 in 308
replace risk_shitstorm = 1 in 311
replace risk_shitstorm = 1 in 313
replace risk_shitstorm = 1 in 323
replace risk_shitstorm = 1 in 326
replace risk_shitstorm = 1 in 328
replace risk_shitstorm = 1 in 330
replace risk_shitstorm = 1 in 334
replace risk_shitstorm = 1 in 343
replace risk_shitstorm = 1 in 346
replace risk_shitstorm = 1 in 349
replace risk_shitstorm = 1 in 350
replace risk_shitstorm = 1 in 352
replace risk_shitstorm = 1 in 361
replace risk_shitstorm = 1 in 366
replace risk_shitstorm = 1 in 372
replace risk_shitstorm = 1 in 373

replace risk_fakenews = 1 in 13
replace risk_fakenews = 1 in 22
replace risk_fakenews = 1 in 38
replace risk_fakenews = 1 in 46
replace risk_fakenews = 1 in 7
replace risk_fakenews = 1 in 103
replace risk_fakenews = 1 in 104
replace risk_fakenews = 1 in 109
replace risk_fakenews = 1 in 123
replace risk_fakenews = 1 in 13
replace risk_fakenews = 1 in 134
replace risk_fakenews = 1 in 22
replace risk_fakenews = 1 in 38
replace risk_fakenews = 1 in 46
replace risk_fakenews = 1 in 65
replace risk_fakenews = 1 in 7
replace risk_fakenews = 1 in 70
replace risk_fakenews = 1 in 74
replace risk_fakenews = 1 in 85
replace risk_fakenews = 1 in 87
replace risk_fakenews = 1 in 92
replace risk_fakenews = 1 in 96
replace risk_fakenews = 1 in 138
replace risk_fakenews = 1 in 141
replace risk_fakenews = 1 in 145
replace risk_fakenews = 1 in 160
replace risk_fakenews = 1 in 163
replace risk_fakenews = 1 in 188
replace risk_fakenews = 1 in 200
replace risk_fakenews = 1 in 207
replace risk_fakenews = 1 in 217
replace risk_fakenews = 1 in 237
replace risk_fakenews = 1 in 251
replace risk_fakenews = 1 in 272
replace risk_fakenews = 1 in 287
replace risk_fakenews = 1 in 297
replace risk_fakenews = 1 in 323
replace risk_fakenews = 1 in 326
replace risk_fakenews = 1 in 330
replace risk_fakenews = 1 in 343
replace risk_fakenews = 1 in 365
replace risk_fakenews = 1 in 370

replace risk_time = 1 in 18
replace risk_time = 1 in 23
replace risk_time = 1 in 24
replace risk_time = 1 in 26
replace risk_time = 1 in 3
replace risk_time = 1 in 31
replace risk_time = 1 in 32
replace risk_time = 1 in 36
replace risk_time = 1 in 43
replace risk_time = 1 in 44
replace risk_time = 1 in 6
replace risk_time = 1 in 101
replace risk_time = 1 in 102
replace risk_time = 1 in 112
replace risk_time = 1 in 116
replace risk_time = 1 in 117
replace risk_time = 1 in 18
replace risk_time = 1 in 23
replace risk_time = 1 in 24
replace risk_time = 1 in 26
replace risk_time = 1 in 3
replace risk_time = 1 in 31
replace risk_time = 1 in 32
replace risk_time = 1 in 36
replace risk_time = 1 in 43
replace risk_time = 1 in 44
replace risk_time = 1 in 55
replace risk_time = 1 in 58
replace risk_time = 1 in 6
replace risk_time = 1 in 66
replace risk_time = 1 in 70
replace risk_time = 1 in 72
replace risk_time = 1 in 84
replace risk_time = 1 in 94
replace risk_time = 1 in 135
replace risk_time = 1 in 138
replace risk_time = 1 in 141
replace risk_time = 1 in 150
replace risk_time = 1 in 154
replace risk_time = 1 in 155
replace risk_time = 1 in 157
replace risk_time = 1 in 160
replace risk_time = 1 in 182
replace risk_time = 1 in 185
replace risk_time = 1 in 186
replace risk_time = 1 in 192
replace risk_time = 1 in 195
replace risk_time = 1 in 196
replace risk_time = 1 in 198
replace risk_time = 1 in 207
replace risk_time = 1 in 209
replace risk_time = 1 in 213
replace risk_time = 1 in 220
replace risk_time = 1 in 229
replace risk_time = 1 in 230
replace risk_time = 1 in 237
replace risk_time = 1 in 241
replace risk_time = 1 in 254
replace risk_time = 1 in 262
replace risk_time = 1 in 272
replace risk_time = 1 in 274
replace risk_time = 1 in 277
replace risk_time = 1 in 295
replace risk_time = 1 in 304
replace risk_time = 1 in 307
replace risk_time = 1 in 310
replace risk_time = 1 in 313
replace risk_time = 1 in 314
replace risk_time = 1 in 316
replace risk_time = 1 in 320
replace risk_time = 1 in 323
replace risk_time = 1 in 329
replace risk_time = 1 in 337
replace risk_time = 1 in 342
replace risk_time = 1 in 344
replace risk_time = 1 in 349
replace risk_time = 1 in 354
replace risk_time = 1 in 355
replace risk_time = 1 in 360
replace risk_time = 1 in 363

replace risk_dataprivacy = 1 in 100
replace risk_dataprivacy = 1 in 112
replace risk_dataprivacy = 1 in 116
replace risk_dataprivacy = 1 in 133
replace risk_dataprivacy = 1 in 35
replace risk_dataprivacy = 1 in 37
replace risk_dataprivacy = 1 in 38
replace risk_dataprivacy = 1 in 41
replace risk_dataprivacy = 1 in 49
replace risk_dataprivacy = 1 in 58
replace risk_dataprivacy = 1 in 78
replace risk_dataprivacy = 1 in 138
replace risk_dataprivacy = 1 in 143
replace risk_dataprivacy = 1 in 153
replace risk_dataprivacy = 1 in 158
replace risk_dataprivacy = 1 in 185
replace risk_dataprivacy = 1 in 209
replace risk_dataprivacy = 1 in 211
replace risk_dataprivacy = 1 in 229
replace risk_dataprivacy = 1 in 236
replace risk_dataprivacy = 1 in 288
replace risk_dataprivacy = 1 in 302
replace risk_dataprivacy = 1 in 307
replace risk_dataprivacy = 1 in 311
replace risk_dataprivacy = 1 in 336
replace risk_dataprivacy = 1 in 345
replace risk_dataprivacy = 1 in 348
replace risk_dataprivacy = 1 in 359
replace risk_dataprivacy = 1 in 362
replace risk_dataprivacy = 1 in 367

* Code who helped with social media (based on variable "Wer_Unterstützt_Sie"
generate sm_help = 0 in 2
replace sm_help = 0 in 108
replace sm_help = 0 in 112
replace sm_help = 0 in 123
replace sm_help = 0 in 131
replace sm_help = 0 in 133
replace sm_help = 0 in 134
replace sm_help = 0 in 137
replace sm_help = 0 in 138
replace sm_help = 0 in 14
replace sm_help = 0 in 160
replace sm_help = 0 in 180
replace sm_help = 0 in 182
replace sm_help = 0 in 183
replace sm_help = 0 in 198
replace sm_help = 0 in 200
replace sm_help = 0 in 207
replace sm_help = 0 in 211
replace sm_help = 0 in 212
replace sm_help = 0 in 213
replace sm_help = 0 in 217
replace sm_help = 0 in 22
replace sm_help = 0 in 229
replace sm_help = 0 in 232
replace sm_help = 0 in 233
replace sm_help = 0 in 237
replace sm_help = 0 in 241
replace sm_help = 0 in 249
replace sm_help = 0 in 26
replace sm_help = 0 in 267
replace sm_help = 0 in 267
replace sm_help = 0 in 268
replace sm_help = 0 in 268
replace sm_help = 0 in 272
replace sm_help = 0 in 28
replace sm_help = 0 in 289
replace sm_help = 0 in 292
replace sm_help = 0 in 295
replace sm_help = 0 in 298
replace sm_help = 0 in 304
replace sm_help = 0 in 308
replace sm_help = 0 in 316
replace sm_help = 0 in 328
replace sm_help = 0 in 342
replace sm_help = 0 in 35
replace sm_help = 0 in 350
replace sm_help = 0 in 352
replace sm_help = 0 in 355
replace sm_help = 0 in 367
replace sm_help = 0 in 42
replace sm_help = 0 in 44
replace sm_help = 0 in 47
replace sm_help = 0 in 55
replace sm_help = 0 in 60
replace sm_help = 0 in 65
replace sm_help = 0 in 74
replace sm_help = 0 in 80
replace sm_help = 0 in 82
replace sm_help = 0 in 85
replace sm_help = 0 in 87
replace sm_help = 0 in 88
replace sm_help = 0 in 93
replace sm_help = 0 in 96
replace sm_help = 1 in 158
replace sm_help = 1 in 186
replace sm_help = 1 in 236
replace sm_help = 1 in 254
replace sm_help = 1 in 266
replace sm_help = 1 in 279
replace sm_help = 1 in 313
replace sm_help = 1 in 339
replace sm_help = 1 in 37
replace sm_help = 2 in 101
replace sm_help = 2 in 102
replace sm_help = 2 in 103
replace sm_help = 2 in 104
replace sm_help = 2 in 105
replace sm_help = 2 in 110
replace sm_help = 2 in 115
replace sm_help = 2 in 117
replace sm_help = 2 in 118
replace sm_help = 2 in 121
replace sm_help = 2 in 124
replace sm_help = 2 in 128
replace sm_help = 2 in 129
replace sm_help = 2 in 136
replace sm_help = 2 in 141
replace sm_help = 2 in 145
replace sm_help = 2 in 149
replace sm_help = 2 in 150
replace sm_help = 2 in 152
replace sm_help = 2 in 153
replace sm_help = 2 in 161
replace sm_help = 2 in 165
replace sm_help = 2 in 166
replace sm_help = 2 in 17
replace sm_help = 2 in 170
replace sm_help = 2 in 171
replace sm_help = 2 in 173
replace sm_help = 2 in 185
replace sm_help = 2 in 190
replace sm_help = 2 in 191
replace sm_help = 2 in 192
replace sm_help = 2 in 202
replace sm_help = 2 in 209
replace sm_help = 2 in 21
replace sm_help = 2 in 215
replace sm_help = 2 in 226
replace sm_help = 2 in 227
replace sm_help = 2 in 253
replace sm_help = 2 in 256
replace sm_help = 2 in 258
replace sm_help = 2 in 265
replace sm_help = 2 in 267
replace sm_help = 2 in 268
replace sm_help = 2 in 270
replace sm_help = 2 in 277
replace sm_help = 2 in 281
replace sm_help = 2 in 287
replace sm_help = 2 in 290
replace sm_help = 2 in 31
replace sm_help = 2 in 310
replace sm_help = 2 in 32
replace sm_help = 2 in 320
replace sm_help = 2 in 324
replace sm_help = 2 in 333
replace sm_help = 2 in 334
replace sm_help = 2 in 343
replace sm_help = 2 in 348
replace sm_help = 2 in 349
replace sm_help = 2 in 354
replace sm_help = 2 in 360
replace sm_help = 2 in 363
replace sm_help = 2 in 365
replace sm_help = 2 in 366
replace sm_help = 2 in 370
replace sm_help = 2 in 372
replace sm_help = 2 in 373
replace sm_help = 2 in 38
replace sm_help = 2 in 48
replace sm_help = 2 in 49
replace sm_help = 2 in 63
replace sm_help = 2 in 70
replace sm_help = 2 in 72
replace sm_help = 2 in 78
replace sm_help = 2 in 84
replace sm_help = 2 in 90
replace sm_help = 2 in 94
replace sm_help = 2 in 96
replace sm_help = 2 in 98
replace sm_help = 3 in 135
replace sm_help = 3 in 148
replace sm_help = 3 in 188
replace sm_help = 3 in 19
replace sm_help = 3 in 273
replace sm_help = 3 in 311
replace sm_help = 3 in 314
replace sm_help = 3 in 330
replace sm_help = 3 in 337
replace sm_help = 3 in 36
replace sm_help = 3 in 361
replace sm_help = 3 in 362
replace sm_help = 3 in 370
replace sm_help = 3 in 79
replace sm_help = 3 in 95

* generate sm_usage
recode SM_Nutzung (2 = 3 "private") (3 = 2 "both job-related and private") (1 = 1 "job-related") (4 = 4 "no social-media usage"), gen(sm_usage)

* generate sm_help2: if freetext "Wer_Unterstützt_Sie" no data --> no help, if Unterstützung_Facebook usw. yes and freetext empty --> staff help
gen sm_help2 = sm_help
replace sm_help2 = 0 if sm_help == .
replace sm_help2 = 2 if sm_help == . & Unterstützung_Facebook == 1
replace sm_help2 = 2 if sm_help == . & Unterstützung_Twitter == 1
replace sm_help2 = 2 if sm_help == . & Unterstützung_Instagram == 1
replace sm_help2 = 2 if sm_help == . & Unterstützung_YouTube == 1
replace sm_help2 = 2 if sm_help == . & Unterstützung_Mess == 1
replace sm_help2 = 2 if sm_help == . & Unterstützung_Xing == 1
replace sm_help2 = 2 if sm_help == . & Unterstützung_LinkedIn == 1
 label define sm_help2 0 "no help" 1 "family" 2 "staff members" 3 "external professionals" 5 "did not answer"
 label values sm_help2 sm_help2

* gen exp_time_sm (expenditure of time for social mediain the job), if social media usage = private or no  --> exp_time_sm == 0
recode Zeitaufwand_SM (5 = 0 "none") (1 = 1  "< 1 hour") (2 = 2 " 1-5 hours") (3 = 3 "5-10 hours") (4 = 4 "> 10 hours"), gen(exp_time_sm)
replace exp_time_sm = 0 if sm_usage >2 

* gen easy_sm (easiness of usage of social media) --> if social media usage = no --> not at all easy
recode Nutzung_Leichtigkeit (6 = 1 "1 (not at all easy)") (5 = 2 "2") (4 = 3 "3") (3 = 4 "4") (2 = 5 "5") (1 = 6 "6 (extremely easy)"), gen(easy_sm)
replace easy_sm = 1 if  sm_usage == 4
label variable easy_sm "easiness of social media usage"

* gen hate speech experienced (dummy)
 gen hate_speech_experienced = .
 replace hate_speech_experienced = 0 if Anfeindungen == 2
 replace hate_speech_experienced = 1 if Anfeindungen == 1
 label define reaction_hate 1 "ignore" 2 "respond (also in PM)" 3 "block/delete post" 4 "complaint"
 label values reaction_hate reaction_hate
 
 * Social Media Utility Index
 recode Bedeutung_SM_ÖA (1 = 6 "very important") (2 = 5 "5") (3 = 4 "4") (4 = 3 "3") (5 = 2 "2") (6 = 1 "not at all important"), gen (sm_relevance_1)
 recode Bedeutung_SM_Austausch (1 = 6 "very important") (2 = 5 "5") (3 = 4 "4") (4 = 3 "3") (5 = 2 "2") (6 = 1 "not at all important"), gen (sm_relevance_2)
 recode Bedeutung_SM_junge_Zielgruppe (1 = 6 "very important") (2 = 5 "5") (3 = 4 "4") (4 = 3 "3") (5 = 2 "2") (6 = 1 "not at all important"), gen (sm_relevance_3)
 recode Bedeutung_SM_eigene_Position (1 = 6 "very important") (2 = 5 "5") (3 = 4 "4") (4 = 3 "3") (5 = 2 "2") (6 = 1 "not at all important"), gen (sm_relevance_4)
 recode Bedeutung_SM_Stimmung_aufnehmen (1 = 6 "very important") (2 = 5 "5") (3 = 4 "4") (4 = 3 "3") (5 = 2 "2") (6 = 1 "not at all important"), gen (sm_relevance_5)
 recode Bedeutung_SM_Bürgerbeteiligung (1 = 6 "very important") (2 = 5 "5") (3 = 4 "4") (4 = 3 "3") (5 = 2 "2") (6 = 1 "not at all important"), gen (sm_relevance_6)
 recode Bedeutung_SM_Krisenkomm_ (1 = 6 "very important") (2 = 5 "5") (3 = 4 "4") (4 = 3 "3") (5 = 2 "2") (6 = 1 "not at all important"), gen (sm_relevance_7)
 recode Bedeutung_SM_Sachverhalte (1 = 6 "very important") (2 = 5 "5") (3 = 4 "4") (4 = 3 "3") (5 = 2 "2") (6 = 1 "not at all important"), gen (sm_relevance_8)
 recode Bedeutung_SM_unterstützung (1 = 6 "very important") (2 = 5 "5") (3 = 4 "4") (4 = 3 "3") (5 = 2 "2") (6 = 1 "not at all important"), gen (sm_relevance_9)
 
 label variable sm_relevance_1 "Öffentlichkeitsarbeit"
 label variable sm_relevance_2 "Austausch mit Bürgern"
 label variable sm_relevance_3 "junge Zielgruppen ansprechen"
 label variable sm_relevance_4 "eigene Positionen vertreten"
 label variable sm_relevance_5 "Stimmungen aus der Bevölkerung aufnehmen"
 label variable sm_relevance_6 "Bürgerbeteiligung stärken"
 label variable sm_relevance_7 "unmittelbare Krisenkommunikation"
 label variable sm_relevance_8 "Sachverhalte erläutern und erklären"
 label variable sm_relevance_9 "Unterstützung für Entscheidungen schaffen"
 
* TABLE O8: test if Social Media Utility Index (SMUI) out of all 9 variables is meaningful:
		pwcorr sm_relevance_1 - sm_relevance_9, obs sig
		factor sm_relevance_1 - sm_relevance_9
		rotate
		estat kmo
		alpha sm_relevance_1 - sm_relevance_9, item
				
	* generate Social Media Utility Index	(SMUI)
	gen sm_relevance_index = (sm_relevance_1 + sm_relevance_2 + sm_relevance_3 + sm_relevance_4 + sm_relevance_5 + sm_relevance_6 + sm_relevance_7 + sm_relevance_8 + sm_relevance_9)/9
 
 * gen party of the mayor
 gen party = Politische_Heimat
 label define party 1 "CDU" 2 "SPD" 3 "FDP" 4 "Greens" 6 "Free Voters" 7 "other party" 8 "no party" 9 "no answer"
 label values party party
 recode party (1 = 1 "CDU") (2 3 4 7 = 3 "other party") (6 = 2 "Free Voters") (8 = 4 "no party") (9 = 5 "no answer"), gen(party2) 

 * gen main job
 gen job = Hauptamt
 label define job 1 "main job" 2 "honorary" 3 "both in more than one community"
 label values job job
 
 gen job2 = .
 replace job2 = 1 if Hauptamt == 1
 replace job2 = 0 if Hauptamt == 2
 replace job2 = 1 if Hauptamt == 3
 label define job2 1 "main job" 0 "honorary" 
 label values job2 job2
 
 * gen size community
 gen size_community = Einwohnerzahl
 label define size_community 1 "< 1" 2 "1-2" 3 "2-3" 4 "3-5" 5 "5-10" 6 "10-20" 7 "20-30" 8 "30-50" 9 "50-150" 10 "> 150"
 label values size_community size_community
 recode size_community (1/4 = 1 "< 5000") (5/6 = 2 "5000-20000") (7/10 = 3 "> 20000"), gen(size_community_3)
 recode size_community (1/3 = 1 "< 3000") (4 = 2 "3000-5000") (5 = 3 "5000-10000") (6 = 4 "10000-20000") (7/10 = 5 "> 20000"), gen(size_community_5)
 
 * gen gender
 gen gender = Geschlecht
 label define gender 1 "male" 2 "female" 3 "non-binary"
 label values gender gender
 gen gender2 = Geschlecht
 replace gender2 = . if Geschlecht == 3
 label values gender2 gender
 recode gender2 (1 = 0 "male") (2 = 1 "female"), gen(female)
 label variable female "female"
 
 * recode age 4 groups
 recode Alter (0/35 = 1 "< 35") (35/44 = 2 "35-44") (45/54 = 3 "45-54") (55/100 = 4 ">=55"), gen(age_4)
 rename Alter age
 label variable age "age"
 
 * recode Facebook group
	recode Facebookgruppe (1 = 1 "yes") (2 = 0 "no") (3 = 99 "don't know"), gen(facebook_group)

* population community aggregated (from survey)
gen pop_aggregated = .
replace pop_aggregated = 1 in 1
replace pop_aggregated = 2 in 2
replace pop_aggregated = 3 in 3
replace pop_aggregated = 4 in 4
replace pop_aggregated = 5 in 5
replace pop_aggregated = 6 in 6
replace pop_aggregated = 7 in 7
replace pop_aggregated = 8 in 8
replace pop_aggregated = 9 in 9
replace pop_aggregated = 10 in 10
label values  pop_aggregated size_community

 
tab size_community, freq matcell(size_comm_bw) 
svmat size_comm_bw

save "data\dump\mayors_survey_workingdata.dta" , replace

 * Zum Vergleich Gesamt BW population
 clear all
 import excel "data\Bevoelkerung Gemeinden 12411-01-01-5.xlsx", sheet("nur BW") firstrow case(lower)
 recode insgesamt (1/1000 = 1 "<1") (1000/2000 = 2 "1-2") (2000/3000 = 3 "2-3") (3000/5000 = 4 "3-5") (5000/10000 = 5 "5-10") (10000/20000 = 6 "10-20") (20000/30000 = 7 "20-30") (30000/50000 = 8 "30-50") (50000/150000 = 9 "50-150") (150000/2000000 = 10 "> 150"), gen(population1)
save "data\dump\population.dta.", replace
 
use  "data\dump\mayors_survey_workingdata.dta"
merge 1:1 _n using "data\dump\population.dta"
drop _merge
save "data\dump\mayors_survey_workingdata.dta", replace

clear all
import excel "data\elections.out.xlsx", sheet("final for Stata") firstrow
recode pop (1/1000 = 1 "<1") (1000/2000 = 2 "1-2") (2000/3000 = 3 "2-3") (3000/5000 = 4 "3-5") (5000/10000 = 5 "5-10") (10000/20000 = 6 "10-20") (20000/30000 = 7 "20-30") (30000/50000 = 8 "30-50") (50000/150000 = 9 "50-150") (150000/2000000 = 10 "> 150"), gen(population2)
save "data\dump\staatsanzeiger_data.dta", replace

use  "data\dump\mayors_survey_workingdata.dta"
merge 1:1 _n using "data\dump\staatsanzeiger_data.dta."
save "data\dump\mayors_survey_workingdata.dta", replace

tab population2, matcell(population_bw2)
svmat population_bw2

tab population1, matcell(population_bw1)
svmat population_bw1

sum(size_comm_bw)
gen sample = 100*size_comm_bw1/r(sum)
sum(population_bw1)
gen reality_pop = 100*population_bw1/r(sum) /// population aus stat. Bundesamt

sum(size_comm_bw1)
gen pop_expected = reality_pop/100 * r(sum)


sum(population_bw2)
gen reality_pop2 = 100 * population_bw2/r(sum) /// population aus Staatsanzeiger Bürgermeisterwahlseite

  
* nach Geschlecht

* sex aggregated label
gen sex_aggregated = .
replace sex_aggregated = 1 in 1
replace sex_aggregated = 2 in 2
replace sex_aggregated = 3 in 3
label values  sex_aggregated gender


tab Geschlecht, matcell(sex_1)
svmat sex_1

recode woman (1 = 2 "weiblich") (0 = 1 "männlich"), gen(Geschlecht_real)
tab Geschlecht_real, matcell(sex_2)
svmat sex_2 

sum(sex_1)
gen sex_sample = 100 * sex_1/r(sum)

sum(sex_2)
gen sex_real = 100 * sex_2/r(sum)

gen sex_chi_sample = sex_11
replace sex_chi_sample = . if sex_aggregated == 3 /// remove the one non-binary person from the sample since this category does not exist in the official data

sum(sex_chi_sample)
gen sex_expected = sex_real/100 * r(sum)

* nach Partei Daten aus Staatsanzeiger Bügermeisterwahlen 2013-2021
gen party_beschriftung = .
replace party_beschriftung = 1 in 1
replace party_beschriftung = 2 in 2
replace party_beschriftung = 3 in 3
replace party_beschriftung = 4 in 4
replace party_beschriftung = 5 in 5
replace party_beschriftung = 6 in 6
replace party_beschriftung = 7 in 7
label define party_beschriftung 1 "CDU" 2 "SPD" 3 "FDP" 4 "Greens" 5 "Freie Wähler" 6 "other party" 7 "no party"
label values party_beschriftung party_beschriftung

tab party, matcell(party_sample)
svmat party_sample
sum(party_sample)
gen party_sample_rel = 100 * party_sample/r(sum)

gen party_chi_sample = party_sample1
replace party_chi_sample = 135+16 in 7
replace party_chi_sample = . in 8

gen party_winner2 = .
replace party_winner2 = 1 if party_winner == "CDU"
replace party_winner2 = 2 if party_winner == "SPD"
replace party_winner2 = 3 if party_winner == "FDP"
replace party_winner2 = 4 if party_winner == "Greens"
replace party_winner2 = 6 if party_winner == "Freie Wähler"
replace party_winner2 = 7 if party_winner == "ÖDP"
replace party_winner2 = 8 if party_winner == "parteilos"
label values party_winner2 party

tab party_winner2, matcell(party_real)
svmat party_real
sum(party_real)
gen party_real_rel = 100 * party_real/r(sum)

sum(party_chi_sample)
gen party_expected = party_real_rel/100 *r(sum)

*****
*** generate variables for chi2-tests (all expected values > 5)

* party
gen party_chi2_sample = . 					
replace party_chi2_sample = 113 in 1		// CDU
replace party_chi2_sample = 21 in 2			// SPD
replace party_chi2_sample = 7 in 3			// FDP/Greens/Other party
replace party_chi2_sample = 68 in 4			// Freie Wähler
replace party_chi2_sample = 151 in 5		// no party/no answer

gen party_chi2_real = .
replace party_chi2_real = 281 in 1		// CDU	
replace party_chi2_real = 54 in 2		// SPD	
replace party_chi2_real = 16 in 3		// FDP/Greens/Other party
replace party_chi2_real = 39 in 4		// Freie Wähler
replace party_chi2_real = 650 in 5		// no party/no answer

sum(party_chi2_real)
gen party_chi2_real_percent = party_chi2_real/r(sum)

sum(party_chi2_sample)
gen party_chi2_expected = party_chi2_real_percent * r(sum)

* population
gen pop_chi2_sample = .
replace pop_chi2_sample = 13  in 1 // < 1000
replace pop_chi2_sample = 29  in 2 // 1000-2000
replace pop_chi2_sample = 40  in 3 // 2000-3000
replace pop_chi2_sample = 93  in 4 // 3000-5000
replace pop_chi2_sample = 106 in 5 // 5000-10000
replace pop_chi2_sample = 53  in 6 // 10000-20000
replace pop_chi2_sample = 10  in 7 // 20000-30000
replace pop_chi2_sample = 10  in 8 // 30000-50000
replace pop_chi2_sample = 8   in 9 // > 50000

gen pop_chi2_real = .
replace pop_chi2_real = 32  in 1
replace pop_chi2_real = 106 in 2
replace pop_chi2_real = 160 in 3
replace pop_chi2_real = 229 in 4
replace pop_chi2_real = 260 in 5
replace pop_chi2_real = 149 in 6
replace pop_chi2_real = 46  in 7
replace pop_chi2_real = 32  in 8
replace pop_chi2_real = 23  in 9

sum(pop_chi2_real)
gen pop_chi2_real_percent = pop_chi2_real/r(sum)

sum(pop_chi2_sample)
gen pop_chi2_expected = pop_chi2_real_percent * r(sum)


* nach Partei vgl. https://www.statistik-bw.de/Service/Veroeff/Monatshefte/PDF/Beitrag19_02_05.pdf
gen party_real2 = .
replace party_real2 = 322 in 1
replace party_real2 = 62 in 2
replace party_real2 = 13 in 3
replace party_real2 = 7 in 4
replace party_real2 = 62 in 5
replace party_real2 = 1 in 6
replace party_real2 = 646 in 7
sum(party_real2)
gen party_real2_rel = 100 * party_real2/r(sum)

save "data\dump\mayors_survey_workingdata.dta.", replace

**** ANALYSIS

	** DESCRIPTIVES**

* FIGURE 2
	* mayors by party / size of the community / main-job / age
	graph bar (count), over(gender) over(party, sort (1) descending  label(angle(30))) asyvars stack ytitle (Number of mayors) title(By party) name(G1, replace) legend(rows(1))

	graph bar (count), over(gender) over(job2, sort (1) descending label(angle(30))) asyvars stack ytitle (Number of mayors) title(By mode of employment) name(G2, replace)
	
	graph bar (count), over(gender) over(size_community, label(angle(30))) ytitle (Number of mayors) asyvars stack title(By size of the community (in thousand inhabitants)) name(G3, replace)
	
	hist age, frequency kdensity bin(15) title(Age distribution) name(G4, replace) xtitle("")
	
	grc1leg G1 G2 G3 G4 , col(2)
	graph export "figures\figure_2.pdf", replace
	graph export "figures\figure_2.emf", replace
 
 
 * FIGURE O1a
	*  Deskriptive Statistiken im Vergleich zu ganz Baden-Württemberg
	graph bar (asis)  sample reality_pop, over(pop_aggregated) ylabel(0 5 10 15 20 25 30 ) ytitle (Percentage of mayors) title(By size of community (in thousand inhabitants)) legend(label(1 "survey sample") label(2 "reality") position(6) ring(3) rows (1)) name(O1a, replace)
 
	graph bar (asis)  sex_sample sex_real, over(sex_aggregated) ytitle (Percentage of mayors) title(By gender) legend(label(1 "survey sample") label(2 "reality (mayors elected 2013-21)") position(6) ring(3) rows (1)) name(O1b, replace)
 
	graph bar (asis) party_sample_rel party_real_rel, over(party_beschriftung) ylabel(0 10 20 30 40 50 60) title (By party affiliation) ytitle (Percentage of mayors) legend(label(1 "survey sample") label(2 "reality (mayors elected 2013-21)") position(6) ring(3) rows(1)) name(O1c, replace)

	graph bar (asis) party_sample_rel party_real2_rel, over(party_beschriftung) ylabel(0 10 20 30 40 50 60) title (By party affiliation) ytitle (Percentage of mayors) legend(label(1 "survey sample") label(2 "reality (mayors elected 2013-21)") position(6) ring(3) rows(1)) name(O1c, replace)
	
	graph combine O1a O1b O1c, rows(3) ysize(8)
	graph export "figures\figure_O1a.pdf", replace
	graph export "figures\figure_O1a.emf", replace

* TABLE O1b	
* Chi-square test for univariate frequency distributions 	
chitest sex_chi_sample sex_expected
chitest party_chi_sample party_expected	
chitest size_comm_bw1 pop_expected

 
 *  FIGURE O2
 *	usage of social media
	* private/for the job
	tab sm_usage
	
	* Social Media Usage by Age
	catplot sm_usage age_4, percent(age_4) asyvars stack ytitle("") legend(position(6) ring(3) colgap(*4)  rows(1)  size(small) ///
	symplacement(center)) title(By Age) name(O2a, replace)
	
	 	* Social Media Usage by Gender
	catplot sm_usage gender2, percent(gender2) asyvars stack ytitle("") legend(position(6) ring(3) rows(1) colgap(*4) size(small) ///
	symplacement(center)) title (By Gender) name(O2b, replace)

	* Social Media Usage by size of municipality
	catplot sm_usage size_community, percent(size_community) asyvars stack ytitle("") legend(position(6) ring(3) rows(1) colgap(*4) size(small) ///
	symplacement(center)) title (By Size of Municipality (in thousand inhabitants)) name(O2c, replace)
	
	* Social Media Usage by Party
	catplot sm_usage party, percent(party) asyvars stack ytitle("") legend(position(6) ring(3) rows(1) colgap(*4) size(small) ///
	symplacement(center)) title (By Party) name(O2d, replace)
 
 	grc1leg O2a O2b O2c O2d,  
	graph export "figures\figure_O2.pdf", replace
	graph export "figures\figure_O2.emf", replace
 
* FIGURE O3 
	* Duration of usage by platform
	catplot SM_Nutzung_Facebook age_4, percent(age_4) asyvars stack ytitle("") legend(position(6) ring(3) colgap(*4)  rows(1)  size(small) symplacement(center) label(1 "not used") label(2 "< 1 year") label(3 "1-3 years") label(4 "4-5 years") label(5 "> 5 years")) title(Facebook (N=245)) name(O3a, replace)
	catplot SM_Nutzung_Messenger age_4, percent(age_4) asyvars stack ytitle("") legend(position(6) ring(3) colgap(*4)  rows(1)  size(small) symplacement(center) label(1 "not used") label(2 "< 1 year") label(3 "1-3 years") label(4 "4-5 years") label(5 "> 5 years")) title(Messenger (N=225)) name(O3b, replace)
	catplot SM_Nutzung_Instagram age_4, percent(age_4) asyvars stack ytitle("") legend(position(6) ring(3) colgap(*4)  rows(1)  size(small) symplacement(center) label(1 "not used") label(2 "< 1 year") label(3 "1-3 years") label(4 "4-5 years") label(5 "> 5 years")) title(Instagram (N=215)) name(O3c, replace)
 	catplot SM_Nutzung_YouTube age_4, percent(age_4) asyvars stack ytitle("") legend(position(6) ring(3) colgap(*4)  rows(1)  size(small) symplacement(center) label(1 "not used") label(2 "< 1 year") label(3 "1-3 years") label(4 "4-5 years") label(5 "> 5 years")) title(YouTube (N=204)) name(O3d, replace)
 	catplot SM_Nutzung_Xing age_4, percent(age_4) asyvars stack ytitle("") legend(position(6) ring(3) colgap(*4)  rows(1)  size(small) symplacement(center) label(1 "not used") label(2 "< 1 year") label(3 "1-3 years") label(4 "4-5 years") label(5 "> 5 years")) title(Xing (N=191)) name(O3e, replace)
	catplot SM_Nutzung_Twitter age_4, percent(age_4) asyvars stack ytitle("") legend(position(6) ring(3) colgap(*4)  rows(1)  size(small) symplacement(center) label(1 "not used") label(2 "< 1 year") label(3 "1-3 years") label(4 "4-5 years") label(5 "> 5 years")) title(Twitter (N=187)) name(O3f, replace)

	grc1leg O3a O3b O3c O3d O3e O3f,  
	graph export "figures\figure_O3.pdf", replace
	graph export "figures\figure_O3.emf", replace
 
 * FIGURE O9
	* Social Media Utility Index - SMUI (histogram and kernel density estimate)
 hist sm_relevance_index, kdens title("") name(G4, replace) xtitle("1 = not at all useful, 6 = extremely useful")
 graph export "figures\figure_O9.pdf", replace
 graph export "figures\figure_O9.emf", replace
 
 * TABLE 2
	* Hate speech against mayors experienced by type of social media usage
	outreg2 hate_speech_experienced sm_usage using "tables/table_2.doc", replace cross label

 

	** REGRESSION MODELS**	
 
clear all 
use  "data\dump\mayors_survey_workingdata.dta"

*** Multiple Imputation hate speech experienced
* keep necessary variables
keep age reaction_hate risk* sm_* hate_speech_exp party party2 job job2 size_comm* female age_4 facebook_group easy_sm exp_time_sm sm_relevance_index
 
* mi dataset erstellen
 mi set flong
 
 * Zusatzvariablen Bevölkerungszahl usw. droppen
 drop if _mi_id > 373
 
*  TABLE O4
*  missing tables
 mi misstable summ female age party2 job2 size_community_5 sm_usage sm_help2 hate_speech_exp facebook_group easy_sm exp_time_sm sm_relevance_index
 mi misstable patterns female age party2 job2 size_community_5 sm_usage sm_help2 hate_speech_exp facebook_group easy_sm exp_time_sm sm_relevance_index
 
 mi register imputed female age party2 job2 sm_usage facebook_group hate_speech size_community_5 easy_sm exp_time_sm sm_relevance_index
 
 * predictive mean matching, since no convergence when nominal variables are imputed via mlogit
* mi impute chained (logit) female hate_speech_exp (mlogit) party2 sm_usage facebook_group (ologit) size_community_5 easy_sm exp_time_sm  (regress) age = sm_help2, add(100) rseed(1492) augment
 mi impute chained (logit)  female hate_speech_exp (pmm, knn(10)) job2 party2 sm_usage facebook_group (ologit) size_community_5 easy_sm exp_time_sm  (regress) age sm_relevance_index = sm_help2, add(100) rseed(1492) augment

 **Substudy 1: Who experienced hate speech
  
	mi estimate , post or: logit hate_speech_experienced  female age i.size_community_5 b4.party2 job2 b4.sm_usage 
	eststo m_1a  
	
	mi estimate , post or: logit hate_speech_experienced  female age i.size_community_5 b4.party2  b99.facebook_group i.exp_time_sm i.sm_help2
	eststo m_1b 
	
	
esttab m_1a m_1b  using "tables\table_3.rtf", eform label  starlevels(+ 0.10 * 0.05 ** 0.01) replace

* TABLE O5
*** Predicted Probabilities
mi estimate , post or saving("data\dump\miestfile.ster", replace) esample(esample): logit hate_speech_experienced  female age i.size_community_5 b4.party2  b99.facebook_group i.exp_time_sm 

mimrgns using "data\dump\miestfile.ster" , esample(esample) predict(pr) at(exp_time_sm = (0(1)4) facebook_group =(0 1 99)) cmdmargins
marginsplot, noci xtitle("Job-related time spent on social media per week") ytitle("Predicted probability") title("") legend(cols(3) position(6) title("Is there an unofficial Facebook group for your municipality?"))
 graph save "figures\figure_O5.gph", replace
 graph export "figures\figure_O5.pdf", replace
 graph export "figures\figure_O5.emf", replace


* TABLE O6 - including Easiness of using Social Media 
	mi estimate , post or: logit hate_speech_experienced  female age i.size_community_5 b4.party2 job2 b4.sm_usage easy_sm
	eststo m_1c
	
	mi estimate , post or: logit hate_speech_experienced  female age i.size_community_5 b4.party2  b99.facebook_group i.exp_time_sm i.sm_help2 easy_sm
	eststo m_1d

esttab  m_1c m_1d using "tables\table_O6.rtf", eform label  starlevels(+ 0.10 * 0.05 ** 0.01) replace
 
  **Substudy 2: Which reactions to hate speech
 
 * based on https://www.statalist.org/forums/forum/general-stata-discussion/general/1489554-stata-se-13-how-to-get-marginsplot-for-all-categories-of-dependent-variable-multiply-imputed-mlogit-using-mimrgns-command
 * create files for combomarginsplot
 
 * Step 1: get margins, saving() file template
  mlogit reaction_hate female age b4.party2  easy_sm if hate_speech_experienced == 1
  margins , at(easy_sm = (1(1)6)) saving("data\dump\margins_saving.dta", replace)
 
 * setp 2: estimate model of interest
 mi estimate, post rrr saving("data\dump\miestfile1.ster", replace) esample(esample1) : mlogit reaction_hate female age b4.party2  easy_sm if hate_speech_experienced == 1
 eststo m_2a
 mi estimate, post rrr saving("data\dump\miestfile2.ster", replace) esample(esample2): mlogit reaction_hate female age b4.party2  i.sm_help2 if hate_speech_experienced == 1
 eststo m_2b
 mi estimate, post rrr saving("data\dump\miestfile3.ster", replace) esample(esample3): mlogit reaction_hate female age b4.party2  i.exp_time_sm  if hate_speech_experienced == 1
 eststo m_2c
 mi estimate, post rrr saving("data\dump\miestfile4.ster", replace) esample(esample4): mlogit reaction_hate female age b4.party2 easy_sm i.sm_help2 i.exp_time_sm  if hate_speech_experienced == 1
 eststo m_2d
  
* TABLE O7 
 esttab m_2a m_2b m_2c m_2d  using "tables\table_O7.rtf",  label eform starlevels(+ 0.10 * 0.05 ** 0.01) replace unstack noomitted nobaselevels 
 
 * FIGURE 3
 * step 3: get predicted values from mimrgns
 forvalues i = 1/4 {
 mimrgns using "data\dump\miestfile1.ster", esample(esample1) at(easy_sm = (1(1)6)) predict(outcome(`i')) 
 matrix rtable = r(table)'
 preserve
 use "data\dump\margins_saving.dta", clear
 svmat rtable
 replace _margin 		= rtable1
 replace _se 			= rtable2
 replace _statistic 	= rtable3
 replace _pvalue 		= rtable4
 replace _ci_lb			= rtable5
 replace _ci_ub 		= rtable6
 drop rtable*
 drop if _margin == .
 save "data\dump\margins_saving`i'.dta", replace
 restore
 }
 
  * step 4: feed the files to combomarginsplot
 combomarginsplot12 "data\dump\margins_saving1.dta" "data\dump\margins_saving2.dta" "data\dump\margins_saving3.dta" "data\dump\margins_saving4.dta" ,  legend(order(5 "ignore" 6 "respond (also in PM)" 7 "block/delete post" 8 "complaint")) xtitle("I consider the use of social media ...") ytitle("Predicted probability") title("") xlabel(1 `" "1"   "not at all easy" "'   2 "2" 3 "3" 4 "4" 5 "5" 6 `" "6" "extremely easy" "' )
 graph save "figures\figure_3.gph", replace
 graph export "figures\figure_3.pdf", replace
 graph export "figures\figure_3.emf", replace
 

** Substudy 3: effects of hate speech on evaluation of social media for political communication 
 mi estimate, post   : reg sm_relevance_index female age b4.party2  i.hate_speech_experienced   
 eststo m_3a
 mi estimate, post  : reg sm_relevance_index female age b4.party2  i.reaction_hate  
 eststo m_3b
 mi estimate, post  : reg sm_relevance_index female age b4.party2  easy_sm   
 eststo m_3c
 mi estimate, post  : reg sm_relevance_index female age b4.party2  i.sm_help2 
 eststo m_3d
 mi estimate,  post : reg sm_relevance_index female age easy_sm b4.party2  i.reaction_hate  i.sm_help2  
 eststo m_3e
 
* TABLE 4
 esttab m_3a m_3b m_3c m_3d m_3e using "tables\table_4.rtf",  label  starlevels(+ 0.10 * 0.05 ** 0.01) replace 
 
 * M3e as coefplot --> not reported 
 coefplot, drop(_cons) xline(0) msymbol(d) mcolor(white)  yscale(alt noline)  ///
     levels( 95 90 ) ciopts(lwidth(3 ..) lcolor( *.4 *.8 ))  ///
	 headings(	1.party2 = `""{bf:Party}" "{bf:(reference: no party)}""'  ///
				2.reaction_hate = `""{bf:Reaction to hate speech}" "{bf:(reference: ignore)}""' ///
				1.sm_help2 = `""{bf:Help with social media}" "{bf:(reference: no one)}""',  labgap(-144)) ///
				graphregion(margin(l=45)) coeflabels(, notick labgap(-140)) ///
     legend(order( 1 "95% CI" 2 "90% CI" ) position(6) rows(1) span)			 
	graph export "data\dump\figure_3.pdf", replace
	graph export "data\dump\figure_3.emf", replace	

** only those who use SM --> not reported
 mi estimate , esampvaryok    : reg sm_relevance_index female age b4.party2  i.hate_speech_experienced if sm_usage < 3  
 
 mi estimate, esampvaryok  : reg sm_relevance_index female age b4.party2  i.reaction_hate if sm_usage < 3   
 
 mi estimate, esampvaryok  : reg sm_relevance_index female age b4.party2  easy_sm   if sm_usage < 3  
 
 mi estimate,  esampvaryok : reg sm_relevance_index female age b4.party2  i.sm_help2 if sm_usage < 3  
 
 mi estimate, esampvaryok  : reg sm_relevance_index female age easy_sm b4.party2  i.reaction_hate  i.sm_help2  if sm_usage < 3  
	

 * TABLE O10
 mi estimate,  post : reg sm_relevance_1 female age easy_sm b4.party2  i.reaction_hate  i.sm_help2 
 eststo m_O10_1
 
  mi estimate,  post : reg sm_relevance_2 female age easy_sm b4.party2  i.reaction_hate  i.sm_help2 
 eststo m_O10_2
 
  mi estimate,  post : reg sm_relevance_3 female age easy_sm b4.party2  i.reaction_hate  i.sm_help2 
 eststo m_O10_3
 
  mi estimate,  post : reg sm_relevance_4 female age easy_sm b4.party2  i.reaction_hate  i.sm_help2 
 eststo m_O10_4
 
  mi estimate,  post : reg sm_relevance_5 female age easy_sm b4.party2  i.reaction_hate  i.sm_help2 
 eststo m_O10_5
 
  mi estimate,  post : reg sm_relevance_6 female age easy_sm b4.party2  i.reaction_hate  i.sm_help2 
 eststo m_O10_6
 
  mi estimate,  post : reg sm_relevance_7 female age easy_sm b4.party2  i.reaction_hate  i.sm_help2 
 eststo m_O10_7
 
  mi estimate,  post : reg sm_relevance_8 female age easy_sm b4.party2  i.reaction_hate  i.sm_help2 
 eststo m_O10_8
 
  mi estimate,  post : reg sm_relevance_9 female age easy_sm b4.party2  i.reaction_hate  i.sm_help2 
 eststo m_O10_9
 
 esttab m_O10_1 m_O10_2 m_O10_3 m_O10_4 m_O10_5 m_O10_6 m_O10_7 m_O10_8 m_O10_9  using "tables\table_O10.rtf",  label  starlevels(+ 0.10 * 0.05 ** 0.01) replace
 
 
 
 
 

